package com.easy.frame.servers.sys.controller;

import com.easy.frame.base.api.pojo.PageDTO;
import com.easy.frame.base.api.pojo.PageVO;
import com.easy.frame.base.basic.ServiceResponse;
import com.easy.frame.base.service.base.BaseController;
import com.easy.frame.servers.sys.client.SysTenantClient;
import com.easy.frame.servers.sys.dto.SysProjectDTO;
import com.easy.frame.servers.sys.dto.SysTenantAddDTO;
import com.easy.frame.servers.sys.dto.SysTenantPageDTO;
import com.easy.frame.servers.sys.dto.SysTenantUpdateDTO;
import com.easy.frame.servers.sys.entity.SysTenant;
import com.easy.frame.servers.sys.service.tenant.SysTenantService;
import com.easy.frame.servers.sys.vo.SysProjectVO;
import com.easy.frame.servers.sys.vo.SysTenantDetailVO;
import com.easy.frame.servers.sys.vo.SysTenantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 租户表相关接口类
 *
 * @author tp
 * @date 2020/12/15 11:39
 */
@Slf4j
@Api(tags = "企业及机构/sys-tenant")
@RestController
@RequestMapping("/sys-tenant")
public class SysTenantController extends BaseController implements SysTenantClient {

    @Resource
    SysTenantService sysTenantService;

    @Override
    @ApiOperation("分页-租户")
    @PostMapping("/page")
    public ServiceResponse<PageVO<SysTenantVO>> page(@RequestBody PageDTO<SysTenantPageDTO> param) {
        PageVO<SysTenantVO> result = sysTenantService.selectForPage(param);
        return success(result);
    }

    @ApiOperation("查询-所有有效租户")
    @GetMapping("/all/tenant")
    public ServiceResponse<List<SysTenant>> getAllTenant() {
        return success(sysTenantService.selectAllTenant());
    }


    @Override
    @ApiOperation("查询-by id")
    @GetMapping("/by/id")
    public ServiceResponse<SysTenantVO> getById(@RequestParam Long id) {
        return success(sysTenantService.selectVOById(id));
    }

    @Override
    @ApiOperation("保存")
    @PostMapping
    public ServiceResponse<?> save(@RequestBody SysTenantAddDTO addDTO) {
        log.info("addDTO的值为：" + addDTO.toString());
        System.out.println(("addDTO的值为：" + addDTO.toString()));
        sysTenantService.add(addDTO);
        return success();
    }

    @ApiOperation("修改")
    @PutMapping
    public ServiceResponse<SysTenantUpdateDTO> update(@RequestBody SysTenantUpdateDTO updateDTO) {
        sysTenantService.updateDetail(updateDTO);
        return success(updateDTO);
    }

    @ApiOperation("删除by id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@RequestParam Long id) {
        SysTenant sysTenant = sysTenantService.selectById(id);
        if (ObjectUtils.isEmpty(sysTenant)) {
            return fail("删除失败，无效数据");
        }
        sysTenantService.updateDeleted(id);
        return success();
    }

    @ApiOperation("租户详情")
    @GetMapping("/detail")
    public ServiceResponse<SysTenantDetailVO> getDetail(@RequestParam Long id) {
        SysTenantDetailVO detail = sysTenantService.getDetail(id);
        return success(detail);
    }

    @ApiOperation("租户对应项目权限分页")
    @PostMapping("/project/page")
    public ServiceResponse<PageVO<SysProjectVO>> getProjectDetailPage(@RequestBody PageDTO<SysProjectDTO> param) {
        PageVO<SysProjectVO> result = sysTenantService.getProjectDetailPage(param);
        return success(result);
    }

    /**
     * ----------------------------------------项目对外二级授权-------------------------------------------------------
     */

    @Override
    @ApiOperation("查询-所有供应商")
    @GetMapping("/all/supplier")
    public ServiceResponse<List<SysTenant>> getListSupplier(@RequestParam Integer supplierType) {
        return success(sysTenantService.listSupplier(supplierType));
    }

}
